package com.example.text;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class LengthHuan extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_length_huan);
        Button button=findViewById(R.id.return3);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Button button1=findViewById(R.id.begin);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText=findViewById(R.id.inputlength);
                String str=editText.getText().toString().trim();
                if(str.isEmpty()){
                    editText.setText("别空着");
                }
                else {
                    boolean judge=true;
                    for(int i=0;i<str.length();i++)
                    {
                        char num=str.charAt(i);
                        if(num!='0'&&num!='1'&&num!='2'&&num!='3'&&num!='4'&&num!='5'&&num!='6'&&num!='7'&&num!='8'
                                &&num!='9'&&num!='.')
                        {
                            editText.setText("请输入数字");
                            judge=false;
                            break;
                        }
                    }
                    if(judge) {
                        Double num = Double.valueOf(str);
                        TextView textView = findViewById(R.id.length_dm);
                        String str1=format(num*0.1);
                        textView.setText(str1 + "(dm)");

                        TextView textView1 = findViewById(R.id.length_m);
                        String str2=format(num*0.01);
                        textView1.setText(str2+ "(m)");

                        TextView textView2 = findViewById(R.id.length_km);
                        String str3=format(num*0.00001);
                        textView2.setText( str3+"(km)");

                        TextView textView3 = findViewById(R.id.length_mm);
                        String str4=format(num*10);
                        textView3.setText(str4+ "(mm)");

                        TextView textView4 = findViewById(R.id.length_um);
                        String str5=format(num * 10000);
                        textView4.setText(str5+ "(um)");

                        TextView textView5 = findViewById(R.id.length_nm);
                        String str6=format(num * 10000000);
                        textView5.setText(str6+ "(nm)");
                    }
                    else {}
                }

            }
        });
    }
    public String format(double value){
        BigDecimal bd=new BigDecimal(value);
        bd=bd.setScale(5, RoundingMode.CEILING.HALF_UP);
        return bd.toString();
    }
}